import { message, notification } from "antd"

export default {
  namespace: "es",

  state: {
    serverEapParams: [], // 数据库中的关于和EAP通信的一些参数.
    serverPcDataCraft: [], //工艺质量表
    serverPcDataManual: [], // 手动参数调整表
    serverPcDataAlarm: [], // 报警表
    serverPcDataEmp: [], // 员工上下机记录上传
    serverPcDataStatus: [], // 不实时采集数据表
    serverPcDataCount: [], // 设备计数表
    serverPcSignalStatus: [], // 设备实时信号表
    serverEapSignalStatus: [], // EAP实时信号表
    serverSizeLayoutData: [] // 显示sizeLayoutData表的数据,从后台获取
  },

  effects: {
    *deleteSizeLayoutData({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        "sqlHdr",
        "delete_size_layout_data",
        [payload]
      )
      if (response === "ok") {
        message.success("数据删除成功", 3)
      } else {
        message.error("数据删除失败: " + response, 5)
      }
    },

    *getSizeLayoutData({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, "sqlHdr", "get_sizelayout_data", [])
    },

    *saveSizeLayoutData({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, "sqlHdr", "save_size_layout_data", [
        payload
      ])
      if (response === "ok") {
        message.success("数据保存成功", 3)
      } else {
        message.error("数据保存失败: " + response, 5)
      }
    },

    *handleSendLotToEap({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, "sqlHdr", "handle_send_lot_to_eap", [
        payload
      ])
      if (response === "ok") {
        message.success("扫码下发成功", 3)
      } else {
        message.error("扫码下发失败: " + response, 5)
      }
    },
    *handleEmpReport({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, "sqlHdr", "add_user_emp_record", [
        payload
      ])
      if (response === "ok") {
        message.success("上报成功", 3)
      } else {
        message.error("上报失败: " + response, 5)
      }
    },

    *handleEapMsgState({ payload }, { call, put }) {
      const { msg } = payload
      console.log("msg.new_value: ", msg.new_value)
      if (msg.val === "updateEapParams") {
        yield put({
          type: "updateEapState",
          payload: msg.new_value
        })
      } else if (msg.val === "updatePcDataCraft") {
        yield put({
          type: "updatePcDataCraft",
          payload: msg.new_value
        })
      } else if (msg.val === "updatePcDataManual") {
        yield put({
          type: "updatePcDataManual",
          payload: msg.new_value
        })
      } else if (msg.val === "updatePcDataAlarm") {
        yield put({
          type: "updatePcDataAlarm",
          payload: msg.new_value
        })
      } else if (msg.val === "updatePcDataEmp") {
        yield put({
          type: "updatePcDataEmp",
          payload: msg.new_value
        })
      } else if (msg.val === "updatePcDataStatus") {
        yield put({
          type: "updatePcDataStatus",
          payload: msg.new_value
        })
      } else if (msg.val === "updatePcDataCount") {
        yield put({
          type: "updatePcDataCount",
          payload: msg.new_value
        })
      } else if (msg.val === "updatePcSignalStatus") {
        yield put({
          type: "updatePcSignalStatus",
          payload: msg.new_value
        })
      } else if (msg.val === "updateEapSignalStatus") {
        yield put({
          type: "updateEapSignalStatus",
          payload: msg.new_value
        })
      } else if (msg.val === "updateSizeLayoutData") {
        yield put({
          type: "updateSizeLayoutData",
          payload: msg.new_value
        })
      } else if (msg.val === "sizeLayoutNotFound") {
        let info = msg.new_value
        notification.open({
          message: "Eap工单下发失败通知",
          description: info,
          onClick: () => {
            console.log("Eap工单下发失败通知被关闭")
          }
        })
      }
    },

    *getEapParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        "sqlHdr",
        "web_request_get_eap_params",
        []
      )
      yield put({
        type: "updateEapState",
        payload: response
      })
    },

    *saveEapParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        "sqlHdr",
        "web_request_save_eap_params",
        [payload]
      )
      if (response == "ok") {
        message.success("保存成功", 3)
      } else {
        message.error("保存失败,失败原因: ", response)
      }
      yield put({
        type: "getEapParams"
      })
    }
  },
  reducers: {
    updateEapSignalStatus(state, action) {
      return {
        ...state,
        serverEapSignalStatus: action.payload
      }
    },
    updatePcSignalStatus(state, action) {
      return {
        ...state,
        serverPcSignalStatus: action.payload
      }
    },
    updatePcDataCount(state, action) {
      return {
        ...state,
        serverPcDataCount: action.payload
      }
    },
    updatePcDataStatus(state, action) {
      return {
        ...state,
        serverPcDataStatus: action.payload
      }
    },
    updatePcDataEmp(state, action) {
      return {
        ...state,
        serverPcDataEmp: action.payload
      }
    },

    updateEapRecipeParams(state, action) {
      return {
        ...state,
        eapRecipeArgs: action.payload
      }
    },
    updateEapConnect(state, action) {
      return {
        ...state,
        eapIsConnect: action.payload
      }
    },
    updateEapMsgShowList(state, action) {
      return {
        ...state,
        eapMsgShowList: action.payload
      }
    },
    updateEapState(state, action) {
      return {
        ...state,
        serverEapParams: action.payload
      }
    },
    updatePcDataCraft(state, action) {
      return {
        ...state,
        serverPcDataCraft: action.payload
      }
    },
    updatePcDataManual(state, action) {
      return {
        ...state,
        serverPcDataManual: action.payload
      }
    },
    updatePcDataAlarm(state, action) {
      return {
        ...state,
        serverPcDataAlarm: action.payload
      }
    },
    updateSizeLayoutData(state, action) {
      return {
        ...state,
        serverSizeLayoutData: action.payload
      }
    },
    clickAddData(state, action) {
      return {
        ...state,
        serverSizeLayoutData: action.payload
      }
    }
  }
}
